package com.cmw.auntapp.component;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmw.auntapp.R;
import com.cmw.auntapp.core.util.CommonUtil;

/**
 * @desc:   订单取消弹窗
 * @author: chengmingwei
 * @date: 2021/5/28 7:57 AM
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */

/**
 * Created by LiBin on 2016/6/16.
 */
public class OrderCancelDialog extends Dialog {

    public OrderCancelDialog(Context context) {
        super(context);
    }

    public OrderCancelDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        private String btnTxt = null;

        private String cancelReason = null;

        Button btnHaveThings;
        Button btnOther;
        private EditText etReason;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public String getValue() {
            String btnOtherTxt = context.getResources().getText(R.string.order_cancel_dialog_btn_txt2).toString();

            if (TextUtils.isEmpty(btnTxt) || btnTxt.equals(btnOtherTxt)) {
                if (etReason.getText() == null) {
                    return null;
                }
                cancelReason = etReason.getText().toString().trim();
            }
            return cancelReason;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        private void init(View parentView) {
            btnHaveThings = parentView.findViewById(R.id.btn_have_things);
            btnOther = parentView.findViewById(R.id.btn_other);
            etReason = parentView.findViewById(R.id.et_reason);
            View.OnClickListener listener = createOnClickListener();
            btnHaveThings.setOnClickListener(listener);
            btnOther.setOnClickListener(listener);
        }

        private View.OnClickListener createOnClickListener() {
            return new View.OnClickListener() {
                @SuppressLint("ResourceAsColor")
                @Override
                public void onClick(View view) {
                    Button currBtn = (Button)view;
                    btnTxt = currBtn.getText().toString();
                    String btnOtherTxt = context.getResources().getText(R.string.order_cancel_dialog_btn_txt2).toString();
                    etReason.setText(null);
                    if (btnTxt.equals(btnOtherTxt)) {
                        cancelReason = null;
                        etReason.setVisibility(View.VISIBLE);
                        btnOther.setBackgroundResource(R.color.colorYellow);
                        btnHaveThings.setBackgroundResource(R.color.colorDDGray);
                    } else {
                        cancelReason = btnTxt;
                        etReason.setVisibility(View.GONE);
                        btnHaveThings.setBackgroundResource(R.color.colorYellow);
                        btnOther.setBackgroundResource(R.color.colorDDGray);
                    }

                }
            };
        }

        public OrderCancelDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final OrderCancelDialog dialog = new OrderCancelDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_order_cancel, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.positiveTextView))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.positiveTextView))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveTextView).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.negativeTextView))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.negativeTextView))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeTextView).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            }
            int screenWidth = CommonUtil.getScreenWidth(context);
            screenWidth = (int) (screenWidth * 0.75);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth, LinearLayout.LayoutParams.MATCH_PARENT);
            ((LinearLayout) layout.findViewById(R.id.content)).setLayoutParams(layoutParams);
            dialog.setContentView(layout);
            init(layout);
            return dialog;
        }
    }
}